<?php

namespace app\api\controller;

use app\mingteng\model\Material;

class Usermaterial extends Base
{
    /**
     * 添加素材
     * @return \think\response\Json
     */
    public function add_material(){
        $token = input('token');
//        echo '<pre>';
//        print_r($token);
//        exit;
        if(empty($token)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $userInfo = $this->checklogin($token);
//        echo '<pre>';
//        print_r($userInfo);
//        exit;
        if(empty($userInfo)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $data = input('post.');
        if(is_array($data['imgs'])){
            $data['imgs'] = implode(',',$data['imgs']);
        }
        $data['user_id'] = $userInfo['user_admin_id'];
        $data['status'] = 1;
        if(Material::create($data)){
            return json([
                'code'=>1,
                'msg'=>"添加成功",
                'time'=>time(),
                'data'=>null
            ]);
        }
        return json([
            'code'=>0,
            'msg'=>"添加失败",
            'time'=>time(),
            'data'=>null
        ]);
    }

    /**
     * 删除素材
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function delete_material(){
        $token = input('token');
        if(empty($token)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $userInfo = $this->checklogin($token);
        if(empty($userInfo)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $id = input('id');
        if(empty($id)){
            return json([
                'code'=>0,
                'msg'=>"请选择要删除的素材",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $data = Material::where('id',$id)
            ->where('user_id',$userInfo['user_admin_id'])
            ->find();
        if(empty($data)){
            return json([
                'code'=>0,
                'msg'=>"该素材不存在,或者不属于您",
                'time'=>time(),
                'data'=>null
            ]);
        }
        if($data->delete()){
            return json([
                'code'=>1,
                'msg'=>"删除成功",
                'time'=>time(),
                'data'=>null
            ]);
        }
        return json([
            'code'=>0,
            'msg'=>"删除失败",
            'time'=>time(),
            'data'=>null
        ]);
    }

    /**
     * 修改素材
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function change_material(){
        $token = input('token');
        if(empty($token)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $userInfo = $this->checklogin($token);
        if(empty($userInfo)){
            return json([
                'code'=>0,
                'msg'=>"请先登录",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $data = input('post.');
        if(empty($data['id'])){
            return json([
                'code'=>0,
                'msg'=>"请选择要修改的素材",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $row = Material::where('id',$data['id'])
                ->where('user_id',$userInfo['user_admin_id'])
                ->find();
        if(empty($row)){
            return json([
                'code'=>0,
                'msg'=>"该素材不存在,或者不属于您",
                'time'=>time(),
                'data'=>null
            ]);
        }
        $data = input('post.');
        if(is_array($data['imgs'])){
            $data['imgs'] = implode(',',$data['imgs']);
        }
        if(isset($data['token'])){
            unset($data['token']);
        }
        if($row->save($data)){
            return json([
                'code'=>1,
                'msg'=>"修改成功",
                'time'=>time(),
                'data'=>null
            ]);
        }
    }

    /**
     * 获取素材
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function get_material(){
        $limit = input('limit',10);
        $data = Material::with(['user'=>function($sql){
            $sql->field('user_admin_id,role,user_name,nickname,imges');
        }])->where('status',1)
            ->order('createtime desc')
            ->paginate($limit);
        return json([
            'code'=>1,
            'msg'=>"获取成功",
            'time'=>time(),
            'data'=>$data
        ]);
    }
}